package cn.xiaobai;

import cn.xiaobai.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @Author yangdaji
 * @Date 2024/2/17
 * @PackageName cn.xiaobai
 * @ClassName SqliteRunTest
 */
@Component
public class SqliteRunTest implements CommandLineRunner {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private UserService userService;


    @Override
    public void run(String... args) throws Exception {
        jdbcTest();
        mybatisTest();
    }


    /**
     * 原生测试
     */
    private void jdbcTest() {
        // 创建表
        jdbcTemplate.execute("create table if not exists user(\n" +
                "  id integer primary key autoincrement,\n" +
                "  name varchar\n" +
                ");\n" +
                "\n");

        // 插入数据
        jdbcTemplate.execute("insert into user(name)\n" +
                "values ('小米'),\n" +
                "       ('华为');");


        // 查询数据
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from user;");
        for (Map<String, Object> map : maps) {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                System.out.print(entry.getKey() + "=" + entry.getValue() + "\t");
            }
            System.out.println();
        }
    }

    /**
     * mybatis 查询
     */
    private void mybatisTest() {
        userService.queryUser();
    }

}
